package com.zxy.ziems.server.alarm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.alarm.convertor.AlarmConvertor;
import com.zxy.ziems.server.alarm.mapper.AlarmTypeMapper;
import com.zxy.ziems.server.alarm.pojo.dto.AlarmTypeDTO;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmTypeEntity;
import com.zxy.ziems.server.alarm.pojo.param.AlarmTypePageParam;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmTypeOptionVO;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmTypeVO;
import com.zxy.ziems.server.alarm.service.AlarmTypeService;
import com.zxy.ziems.server.domain.User;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.service.UserService;
import com.zxy.ziems.server.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 告警管理
 *
 * @author chdzq
 * @create 2023/3/28
 */
@Service
public class AlarmTypeServiceImpl extends ServiceImplX<AlarmTypeMapper, AlarmTypeEntity> implements AlarmTypeService {

    private void doCheckBeforeSaveAction(AlarmTypeDTO dto) {
        if (StrUtil.isNotBlank(dto.getName())) {
            AlarmTypeEntity old = getByName(dto.getName());
            AssertUtil.isFalse(null != old && !Objects.equals(old.getId(), dto.getId()), "告警类型已存在");
        }
    }

    @Override
    public void add(AlarmTypeDTO dto) {
        doCheckBeforeSaveAction(dto);
        if (StrUtil.isBlank(dto.getDescription())) {
            dto.setDescription(dto.getName());
        }
        AlarmTypeEntity entity = AlarmConvertor.INSTANCE.typeDTOConvert2DO(dto);
        save(entity);
    }

    @Override
    public void update(AlarmTypeDTO dto) {
        doCheckBeforeSaveAction(dto);
        AlarmTypeEntity entity = AlarmConvertor.INSTANCE.typeDTOConvert2DO(dto);
        updateById(entity);
    }

    @Override
    public void deleteById(String id) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        super.removeById(id);
    }

    @Override
    public AlarmTypeEntity getByName(String name) {
        if (StrUtil.isBlank(name)) {
            return null;
        }
        return getOne(AlarmTypeEntity::getName, name);
    }


    @Override
    public AlarmTypeEntity getById(String id) {
        if (StrUtil.isBlank(id)) {
            return null;
        }
        return super.getById(id);
    }

    @Override
    public List<AlarmTypeEntity> listByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return new ArrayList<>(0);
        }
        return super.listByIds(idList);
    }

    @Override
    public AlarmTypeVO detailById(String id) {
        AlarmTypeEntity entity = getById(id);
        User updateUser = Objects.isNull(entity.getUpdateUserId()) ? null : userService.getById(entity.getUpdateUserId());
        AlarmTypeVO vo = AlarmConvertor.INSTANCE.typeDOConvert2VO(entity, updateUser);
        return vo;
    }

    @Override
    public PageResult<AlarmTypeVO> pageListBy(AlarmTypePageParam param) {
        PageResult<AlarmTypeEntity> result = customPage(param, (page, aPageParam) -> baseMapper.queryPageList(page, aPageParam));
        List<AlarmTypeVO> vos = AlarmConvertor.INSTANCE.typeDOListConvert2VOList(result.getList());
        return new PageResult<>(result.getTotal(), vos);
    }

    @Override
    public List<AlarmTypeVO> viewList() {
        List<AlarmTypeEntity> list = list();
        Set<String> userIdSet = list.stream().map(AlarmTypeEntity::getUpdateUserId).collect(Collectors.toSet());
        List<User> users = userService.listByIds(userIdSet);
        Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getUserId, (a) -> a));

        AlarmConvertor instance = AlarmConvertor.INSTANCE;

        return list
                .stream()
                .map((entity) -> instance.typeDOConvert2VO(
                        entity,
                        Objects.nonNull(entity.getUpdateUserId()) ? userMap.get(entity.getUpdateUserId()) : null))
                .collect(Collectors.toList());
    }

    @Override
    public List<AlarmTypeOptionVO> viewOptionList() {
        List<AlarmTypeEntity> list = list();
        return AlarmConvertor.INSTANCE.typeDOConvert2VOList(list);
    }

    @Override
    public AlarmTypeEntity getByNameAndAdd(String name) {
        AlarmTypeEntity res = this.getByName(name);
        if (!Objects.isNull(res)) {
            return res;
        }
        AlarmTypeEntity entity = new AlarmTypeEntity();
        entity.setName(name);
        entity.setDescription(name);
        save(entity);
        return this.getByName(name);
    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
